package cn.lxg.controller.user;

import cn.lxg.pojo.TNovel;
import cn.lxg.pojo.TNovelChapter;
import cn.lxg.pojo.TNovelGroup;
import cn.lxg.service.INovelChapterService;
import cn.lxg.service.INovelGroupService;
import cn.lxg.service.INovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller(value = "userNovelController")
@RequestMapping(value = "/page/user")
public class NovelController {
    @Autowired
    private final INovelService novelService = null;

    @Autowired
    private final INovelGroupService novelGroupService = null;

    @Autowired
    private final INovelChapterService novelChapterService = null;

    @GetMapping("/novel_list")
    public String listPage(
            Model model,
            @RequestParam(value = "limit", required = false, defaultValue = "15") Integer limit,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            TNovel novel) {
        // 预处理参数
        if (novel.getName() != null)
            novel.setName(novel.getName().trim());
        if (novel.getSynopsis() != null)
            novel.setSynopsis(novel.getSynopsis().trim());
        TNovelGroup novelGroup = new TNovelGroup();

        // 准备数据
        List<TNovel> novelList = this.novelService.getList(limit, page, novel);
        List<TNovelGroup> novelGroupList = this.novelGroupService.getList(null, null, novelGroup);
        int dataCount = this.novelService.countDataLine(novel);
        int pageIndex = page;
        int pageCount = (int) Math.ceil((float) dataCount / limit);

        // 传递数据
        model.addAttribute("novel", novel);
        model.addAttribute("novelList", novelList);
        model.addAttribute("novelGroupList", novelGroupList);
        model.addAttribute("dataCount", dataCount);
        model.addAttribute("defaultLimit", limit);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageCount", pageCount);

        return "user/novel/list";
    }

    @GetMapping(value = "/read_novel/{id}/{page}")
    public String readPage(
            Model model,
            @PathVariable(value = "id") Integer id,
            @PathVariable(value = "page") Integer page) {
        // 预处理数据
        TNovelChapter novelChapter = new TNovelChapter();
        novelChapter.setNovelId(id);

        // 准备数据
        TNovel novel = this.novelService.get(id);
        List<TNovelChapter> novelChapterList = this.novelChapterService.getList(null, null, novelChapter);
        if (novelChapterList.size() < page)
            page = novelChapterList.size();

        // 传输数据给前端
        model.addAttribute("novel", novel);
        model.addAttribute("novelChapterList", novelChapterList);
        model.addAttribute("page", page);

        return "user/novel/read";
    }
}
